<?php

/**
 * @author Terry & 千里孤坟
 * @copyright TH Studio/chinafe
 */
	require_once("class.php");
	require_once("order.php");
	session_start(); 	
	
	require_once "config.php";
	
	mt_srand((double)microtime() * 1000000);
	
	$_SESSION['save'] = array();
	$_SESSION['info'] = array();
	$_SESSION['turn'] = 1;	
	$_SESSION['extrainfo']['boss_fight'] = 0;
	$_SESSION['extrainfo']['a_point'] = 0;
 	$_SESSION['extrainfo']['b_point'] = 0;
	$_SESSION['extrainfo']['saved'] = 0;
  	$a = array(0 => new Char,1 => new Char,2 => new Char,3 => new Char);  //A组队员
  	$b = array(0 => new Char,1 => new Char,2 => new Char,3 => new Char);  //B组队员
	$orderlist = array();
 	
 	for($i = 0;$i < 4;$i ++) {
		$a[$i]->cid = $_POST['acid'][$i];
		if($a[$i]->cid < 1) {
			unset($a[$i]);
			continue;			
		}
		$tn = $a[$i]->name = $_POST['aname'][$i];
		$a[$i]->uuid = md5($a[$i]->cid."A".$i);
		$a[$i]->eodds = 15;	//基础逃跑成功率
		$a[$i]->escaping = 0;
		
		$a[$i]->mxhp = $_POST['ahp'][$i];
		$a[$i]->hp = $_POST['ahp'][$i];
		$a[$i]->mxsp = $_POST['asp'][$i];
		$a[$i]->sp = $_POST['asp'][$i];
		$a[$i]->ap = 6;
		$a[$i]->sap = 0;
		$a[$i]->str = $_POST['astr'][$i];
		$a[$i]->mgr = $_POST['amgr'][$i];
		$tspd = $a[$i]->spd = $_POST['aspd'][$i];
		$a[$i]->vit = $_POST['avit'][$i];
		$a[$i]->luk = $_POST['aluk'][$i];
		
		$rs=$db->execute("select * from cmember where cid = ".$a[$i]->cid);
		while($r=$db->fetch_array($rs)) {
			if(substr($a[$i]->name,0,4) == "BOSS") {
				CalHelper::boss_init($a[$i],$r);
			} else {
				$a[$i]->gender = $r['gender'];
				$a[$i]->element = $r['element'];
				$a[$i]->baselv = $r['baselv'];
				$a[$i]->level = 99;
				$a[$i]->groupid = $r['pid'];
				$a[$i]->groupname = $r['cgroup'];
				if($a[$i]->name == "") $tn = $a[$i]->name = $r['cmember'];
			}
		}
		$a[$i]->rouseskcount = 0;	//觉醒技能发动计数器
		$a[$i]->groupskcount = 0;	//组别技能发动计数器
		$a[$i]->lasthp = $a[$i]->mxhp;	//记录上回合HP，默认是mxhp
		$a[$i]->sapool = 20;	//最大额外获得SAP
		$orderlist[$tn] = $tspd;
		StatusHelper::uskill_init($a[$i]);	//专有特定技能启动
		$db->free_result($rs);
	}
	
	for($i = 0;$i < 4;$i ++) {
		$b[$i]->cid = $_POST['bcid'][$i];
		if($b[$i]->cid < 1) {
			unset($b[$i]);
			continue;			
		}

		$tn = $b[$i]->name = $_POST['bname'][$i];
		$b[$i]->uuid = md5($b[$i]->cid."B".$i);
		$b[$i]->eodds = 15;	//基础逃跑成功率
		$b[$i]->escaping = 0;

		$b[$i]->mxhp = $_POST['bhp'][$i];
		$b[$i]->hp = $_POST['bhp'][$i];
		$b[$i]->mxsp = $_POST['bsp'][$i];
		$b[$i]->sp = $_POST['bsp'][$i];
		$b[$i]->tp = $b[$i]->mxtp = 999; //TODO: clean it.
		$b[$i]->ap = 6;
		$b[$i]->sap = 0;
		$b[$i]->str = $_POST['bstr'][$i];
		$b[$i]->mgr = $_POST['bmgr'][$i];
		$tspd = $b[$i]->spd = $_POST['bspd'][$i];
		$b[$i]->vit = $_POST['bvit'][$i];
		$b[$i]->luk = $_POST['bluk'][$i];
		
		$rs=$db->execute("select * from cmember where cid = ".$b[$i]->cid);
		while($r=$db->fetch_array($rs)) {
			if(substr($b[$i]->name,0,4) == "BOSS") {
				CalHelper::boss_init($b[$i],$r);
			} else {
				$b[$i]->gender = $r['gender'];
				$b[$i]->element = $r['element'];
				$b[$i]->baselv = $r['baselv'];
				$b[$i]->level = 99;
				$b[$i]->groupid = $r['pid'];
				$b[$i]->groupname = $r['cgroup'];
				if($b[$i]->name == "") $tn = $b[$i]->name = $r['cmember'];
			}
		}
		$b[$i]->rouseskcount = 0;	//觉醒技能发动计数器
		$b[$i]->groupskcount = 0;	//组别技能发动计数器
		$b[$i]->sapool = 20;	//最大额外获得SAP
		$b[$i]->lasthp = $b[$i]->mxhp;	//记录上回合HP，默认是mxhp
		$orderlist[$tn] = $tspd;
		StatusHelper::uskill_init($b[$i]);	//专有特定技能启动
		$db->free_result($rs);
	}
	
	//有人数差时的AP加成
	$ca = count($a);
	$cb = count($b);
	$extra_ap = 7 * abs($ca-$cb);
	if($ca > $cb) {
		foreach($b as $bb) {
			$bb->ap += $extra_ap;
			$bb->sap += $extra_ap;
		}
	} elseif($ca < $cb) {
		foreach($a as $aa) {
			$aa->ap += $extra_ap;
			$aa->sap += $extra_ap;
		}
	}
	
	$_SESSION['a'] = $a;
 	$_SESSION['b'] = $b;
	

	//场景密码处理
	if(empty($_POST['tpass'])) {
		$force = 0;
	} elseif($_POST['tpass'] == substr(md5(date('Y-m-d')),10,7)) {
		$force = 1;	//火花帝国城下町
	} elseif($_POST['tpass'] == substr(md5(date('d-m-Y')),11,7)) {
		$force = 2;	//克里特岛
	} elseif($_POST['tpass'] == substr(md5(date('m-Y-d')),12,7)) {
		$force = 3;	//凡尔赛宫
	} elseif($_POST['tpass'] == substr(md5(date('d-Y-m')),13,7)) {
		$force = 4;	//罗马斗技场
	} elseif($_POST['tpass'] == substr(md5(date('m-d-Y')),14,7)) {
		$force = 5;	//格拉那达
	} elseif($_POST['tpass'] == substr(md5(date('Y-d-m')),15,7)) {
		$force = 6;	//巴黎圣母院大教堂
	} elseif($_POST['tpass'] == substr(md5(date('Y-d-Y')),16,7)) {
		$force = 7;	//恒河
	} elseif($_POST['tpass'] == substr(md5(date('Y-Y-d')),17,7)) {
		$force = 8;	//巴特农神庙
	} elseif($_POST['tpass'] == substr(md5(date('d-Y-Y')),18,7)) {
		$force = 9;	//胡夫金字塔
	} elseif($_POST['tpass'] == substr(md5(date('Y-d-d')),19,7)) {
		$force = 10;	//巴比伦空中花园
	} elseif($_POST['tpass'] == substr(md5(date('d-d-Y')),20,7)) {
		$force = 11;	//秦始皇陵墓
	} elseif($_POST['tpass'] == substr(md5(date('m-m-d')),21,7)) {
		$force = 12;	//巴别塔
	} elseif($_POST['tpass'] == substr(md5(date('d-Y-d')),22,7)) {
		$force = 13;	//迦南美地
	} else {
		$force = 0;
	}
 	
	//场景确定
	$_SESSION['T'] = ($force == 0) ? $_POST['terrain'] : $force;
 
 	$_SESSION['result'] = "&nbsp;";
	$_SESSION['show'] = "&nbsp;";
	$_SESSION['extrainfo']['orderlist'] = get_order($orderlist,3);
	$_SESSION['extrainfo']['ooduel'] = (CalHelper::isonetone()) ? 1 : 0;	//判断是否1v1
	$_SESSION['extrainfo']['talkhistory'] = array();
	$_SESSION['extrainfo']['warhistory'] = array();
	$_SESSION['extrainfo']['warhistory']['startime'] = date('Y-m-d H:i:s');  
 	
	header('Location: battle.php');	
?>
<html>
<head>

</head>
<body>

</body>

</html>
